package Demo01;

import java.lang.reflect.Array;
import java.util.Arrays;

import javax.sound.midi.Soundbank;

public class Test {
    //得到一个长度为n的数组
    //数组的数字在1~n之间,随机得到
    public static int[] randomArray(int n, int v) {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = (int)(Math.random()*v)+1;
        }
        return arr;
    }
    public static int[] copyArray(int[] arr) {
        int n = arr.length;
        int[] ans = new int[n];
        for (int i = 0; i < n; i++) {
            ans[i] = arr[i];
        }
        return ans;
    }

    public static void main(String[] args) {
        /*
         * int[] arr = {1,3,7,1,3,2,6,1,7,4};
         *
         * System.out.println(Arrays.toString(arr)); // selectionSort(arr); //
         * bubbleSort(arr); InsertSort(arr); System.out.println(Arrays.toString(arr));
         */
        //数组长度
        int N = 100;
        //数字范围1~V
        int V = 1000;
        //测试次数
        int testTimes = 50000;
        System.out.println("测试开始");
        for (int i = 0; i < testTimes; i++) {
            //生成一个长度,长度在[0~N-1]
            int n = (int)(Math.random()*N);
            int[] arr = randomArray(n, V);
            int[] arr1 = copyArray(arr);
            int[] arr2 = copyArray(arr);
            int[] arr3 = copyArray(arr);
            selectionSort(arr1);
            bubbleSort(arr2);
            InsertSort(arr3);
            if(!sameArray(arr1, arr2)||!sameArray(arr1, arr3)) {
                System.out.println("出错了!!!");
                System.out.println(Arrays.toString(arr));
                System.out.println(Arrays.toString(arr1));
                System.out.println(Arrays.toString(arr2));
                System.out.println(Arrays.toString(arr3));

                break;
            }
        }
        System.out.println("测试结束");
    }
    public static boolean sameArray(int[] arr1, int[] arr2) {
        for (int i = 0; i < arr2.length; i++) {
            if(arr1[i]!=arr2[i]) {
                return false;
            }
        }
        return true;
    }
    public static void InsertSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int index = i;
            int tem = arr[index];
            while(index>0) {
                if(arr[index]<arr[index-1]) {
                    swap(arr, index, index-1);
                    index--;
                }
                else {
                    break;
                }
            }
            arr[index] = tem;
        }
    }
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if(arr[j]>arr[j+1]) {
                    swap(arr, j, j+1);
                }
            }
        }
    }
    public static void selectionSort(int[] arr) {
        if(arr==null||arr.length<2) {
            return;
        }
        for (int i = 0; i < arr.length-1; i++) {
            int minIndex = i;
            for (int j = i; j < arr.length; j++) {
                if(arr[j]<arr[minIndex]) {
                    minIndex = j;
                }
            }
            swap(arr,minIndex,i);
        }
    }
    public static void swap(int[] arr, int x, int y) {
        int tem = arr[x];
        arr[x] = arr[y];
        arr[y] = tem;
    }
}
